package com.appspot.gaeforum309.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.gaeforum309.core.ForumAPI;
import com.appspot.gaeforum309.db.DBUser;
import com.appspot.gaeforum309.pathing.Action;
import com.appspot.gaeforum309.pathing.PathBuilder;
import com.appspot.gaeforum309.pathing.PathableClass;
import com.appspot.gaeforum309.pathing.Action.Field;
import com.appspot.gaeforum309.pathing.PathBuilder.IPathable;
import com.appspot.gaeforum309.web.ForumPages.Page;

// TODO: Find a similare solution of Pathing to ForumAction, to found URL.
// For example:
//   -IPathable object have a method which return true if adding child is authorized or not(can depends of current user, ...).
//   -Template is in charge of test it before showing a forms
//   -ForumAction generate an URL and a list of hidden key/value shall be present on the form (which will have tagName, tagKey, ...).
//   -Template shall add a redirect URL, which can be:
//     -Classical URL (http://...)
//     -Tagname/Tagkey ref (GAEForum:tagName=xxx&tagKey=xxx)
//     -New created object (GAEForum:newObject)
//      
@SuppressWarnings("serial")
public class ForumAction extends HttpServlet {
	private static final String forumActionUrl = "/forumAction";
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if(! ForumAPI.loggedUser())
		{
			// TODO: Catch this error
			return;
		}
		
		DBUser user = ForumAPI.userLogin();
		if(user == null)
		{
			// TODO: Catch this error
			return;
		}
		
		String gaeAction = req.getParameter("gaeAction");
		String tagName = req.getParameter("gaeTagName");
		String tagKey = req.getParameter("gaeTagKey");
		String redirect = req.getParameter("redirect");
		
		if(gaeAction == null || tagName == null || tagKey == null)
		{
			// TODO: Catch this error
			return;
		}
		
		if(redirect == null)
		{
			redirect = Page.getUrl(tagName, tagKey);
		}
		
		Action.ActionType actionType = Action.ActionType.valueOf(gaeAction);
		
		if(actionType == null)
		{
			// TODO: Catch this error
			return;
		}
		
		// Rebuild action
		PathableClass pc = PathBuilder.getPathableClass(tagName);
		if(pc == null)
		{
			// TODO: Catch this error
			return;
		}
		
		IPathable ip = pc.getObject(tagKey);
		if(ip == null)
		{
			// TODO: Catch this error
			return;
		}
		
		// Rebuild action object with value from form.
		Action action = Action.buildAction(ip, actionType);
		if(action == null)
		{
			// TODO: Catch this error, operation not permit
			return;
		}
		
		for(Field field:action.getFieldsList())
		{
			String value = req.getParameter(field.name);
			if(value != null)
			{
				field.defaultValue = value;
			}
		}
		
		if(!ip.applyAction(action))
		{
			// TODO: Found a solution to send failure return to the remplate, in the redirection
		}
		
		resp.sendRedirect(redirect);
	}
}
